import { getToken } from '../auth'
const base_url = (window as any).config.base_url
const token = getToken()
var utlis = {
	Percentage: (sum, total, flag) => {
		// flag 是否需要%号
		sum = parseFloat(sum)
		total = parseFloat(total)
		if (isNaN(sum) || isNaN(total)) {
			return '-'
		}
		if (flag) {
			return total <= 0 ? '0%' : (Math.round(sum / total * 10000) / 100.0).toFixed(0) + '%'
		} else {
			return total <= 0 ? '0' : parseInt(Math.round(sum / total * 10000) / 100.0).toFixed(0)
		}
	},

	groupBy(array, f) {
		const groups = {}
		array.forEach(function(o) {
			const group = f(o)
			groups[group] = groups[group] || []
			groups[group].push(o)
		})
		return Object.keys(groups).map(function(group) {
			return { key: group, val: groups[group] }
		})
	},
}

export const getImg = (mapId) => {
	return `${base_url}/Maps/pic/${mapId}?access_token=${token}`
}

export default utlis
